/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.ddtroops.web.actionbeans;

import cz.muni.fi.pa165.ddtroops.dto.SkillDTO;
import cz.muni.fi.pa165.ddtroops.serviceinterfaces.SkillService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;

/**
 *
 * @author newohybat
 */
@UrlBinding("/skills/{$event}/{skill.id}")
public class SkillActionBean implements ActionBean{

    private ActionBeanContext context;

    @SpringBean(value="skillWebService")
    private SkillService service;

    @Override
    public void setContext(ActionBeanContext abc) {
        context=abc;
    }

    @Override
    public ActionBeanContext getContext() {
        return context;
    }

    private SkillDTO skill;

    @ValidateNestedProperties(
        value= {
            @Validate(on={"add", "save"},field="name", required=true),
            @Validate(on={"add", "save"},field="description", required=true),
            @Validate(on={"add", "save"},field="profession", required=true),
            @Validate(on={"add", "save"},field="minXP", required=true, minvalue=0)
        }
    )
    public SkillDTO getSkill(){
        return skill;
    }

    public void setSkill(SkillDTO skill){
        this.skill = skill;
    }

    @DefaultHandler
    public Resolution skills(){
        return new ForwardResolution("/skills.jsp");
    }

    public List<SkillDTO> getSkills()
    {
        return service.getAll();
    }
    
    public Set<String> getProffessions(){
        Set<String> l = new HashSet<String>();
        for (SkillDTO s : service.getAll())
            l.add(s.profession);
        return l;
    }
    
    public List<SkillDTO> getSkillsOfProffession(String p){
        return service.getByProfession(p);
    }

    public Resolution add(){
        service.create(skill);
        return new RedirectResolution(this.getClass(), "all");
    }

    public Resolution delete(){
        service.delete(skill);
        return new RedirectResolution(this.getClass(), "all");

    }

    @Before(stages = LifecycleStage.BindingAndValidation, on = {"edit", "save", "delete"})
    public void loadSkillFromDb(){
        String ids = context.getRequest().getParameter("skill.getId()");
        if (ids == null) return;
        skill = service.getById(Long.parseLong(ids));
    }

    public Resolution edit(){
        return new ForwardResolution("/skillsEdit.jsp");
    }

    public Resolution save(){
        service.update(skill);
        return new RedirectResolution(this.getClass(), "all");
    }
}
